package com.ruipengkj.commons.dao;

import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.util.LinkedHashMap;
import java.util.List;

import com.ruipengkj.commons.base.dao.BaseDaoSupport;
import com.ruipengkj.commons.base.query.PageModel;

public abstract class GenericDao<T, ID extends Serializable> extends BaseDaoSupport<T> implements IGenericDao<T,ID>{

	
	private Class<T> persistentClass;

	@SuppressWarnings("unchecked")
	public GenericDao() {
		// 获取持久化对象的类型
		this.persistentClass = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
	}

	public Class<T> getPersistentClass() {
		return persistentClass;
	}
	
	@Override
	public void deleteDao(ID id) throws RuntimeException {
		this.deleteObject(this.getPersistentClass(), id);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<T> findDao(String hql, String whereHQL, List<Object> param,
			LinkedHashMap<String, String> orderby) throws RuntimeException {
		return (List<T>)this.find(hql, whereHQL, param, orderby);
	}

	@Override
	public T getByIdDao(ID id) throws RuntimeException {
		return this.getById(this.getPersistentClass(), id);
	}

	@Override
	public PageModel<T> getScrollDao(String hql, String whereHQL,
			List<Object> param, int firstResult, int maxResult,
			LinkedHashMap<String, String> orderby) throws RuntimeException {
		return this.getScrollData(hql, whereHQL, param, firstResult, maxResult, orderby);
	}

	@Override
	public void modifyDao(T entity) throws RuntimeException {
		this.updateObject(entity);
	}

	@Override
	public void saveDao(T entity) throws RuntimeException {
		this.saveObject(entity);
	}

	@Override
	public Serializable saveRetDao(T entity) throws RuntimeException {
		return this.saveObjectRet(entity);
	}

	@Override
	public void saveOrUpdateDao(T entity) throws RuntimeException {
		this.insertOrUpdate(entity);
	}
	
	@Override
	public int executeSQL(String sql) {
		return super.executeSQL(sql);
	}

	@Override
	public List<Object[]> querySQL(String sql) {
		return super.querySQL(sql);
	}
}
